package cn.iocoder.yudao.module.written.exam.utils;

import cn.iocoder.yudao.module.written.exam.dal.dataobject.llm.*;
import cn.iocoder.yudao.module.written.exam.controller.app.writtenexam.vo.llm.LlmChatRespVO;

/**
 * 大模型事件工具类
 *
 * @author 芋道源码
 */
public class LlmEventUtil {

    /**
     * 创建消息事件
     *
     * @param conversationId 会话ID
     * @param messageId      消息ID
     * @param taskId         任务ID
     * @param answer         答案内容
     * @return MessageEvent对象
     */
    public static MessageEvent createMessageEvent(String conversationId, String messageId,
                                                  String taskId, String answer) {
        MessageEvent messageEvent = new MessageEvent();

        // 设置基础属性
        messageEvent.setEvent(LlmEventTypeEnum.MESSAGE.getType());
        messageEvent.setConversationId(conversationId);
        messageEvent.setMessageId(messageId);
        messageEvent.setTaskId(taskId);
        messageEvent.setCreatedAt(System.currentTimeMillis());

        // 设置消息特有属性
        messageEvent.setId(messageId);
        messageEvent.setAnswer(answer);

        return messageEvent;
    }

    /**
     * 创建消息结束事件
     *
     * @param conversationId 会话ID
     * @param messageId      消息ID
     * @param taskId         任务ID
     * @param metadata       元数据
     * @return MessageEndEvent对象
     */
    public static MessageEndEvent createMessageEndEvent(String conversationId, String messageId,
                                                        String taskId, Metadata metadata) {
        MessageEndEvent messageEndEvent = new MessageEndEvent();

        // 设置基础属性
        messageEndEvent.setEvent(LlmEventTypeEnum.MESSAGE_END.getType());
        messageEndEvent.setConversationId(conversationId);
        messageEndEvent.setMessageId(messageId);
        messageEndEvent.setTaskId(taskId);
        messageEndEvent.setCreatedAt(System.currentTimeMillis());

        // 设置消息结束特有属性
        messageEndEvent.setId(messageId);
        messageEndEvent.setMetadata(metadata);

        return messageEndEvent;
    }

    /**
     * 创建工作流开始事件
     *
     * @param conversationId 会话ID
     * @param messageId      消息ID
     * @param taskId         任务ID
     * @param workflowRunId  工作流运行ID
     * @param data           工作流数据
     * @return WorkflowStartedEvent对象
     */
    public static WorkflowStartedEvent createWorkflowStartedEvent(String conversationId, String messageId,
                                                                  String taskId, String workflowRunId,
                                                                  WorkflowStartedData data) {
        WorkflowStartedEvent event = new WorkflowStartedEvent();

        // 设置基础属性
        event.setEvent(LlmEventTypeEnum.WORKFLOW_STARTED.getType());
        event.setConversationId(conversationId);
        event.setMessageId(messageId);
        event.setTaskId(taskId);
        event.setCreatedAt(System.currentTimeMillis());

        // 设置工作流特有属性
        event.setWorkflowRunId(workflowRunId);
        event.setData(data);

        return event;
    }

    /**
     * 创建工作流完成事件
     *
     * @param conversationId 会话ID
     * @param messageId      消息ID
     * @param taskId         任务ID
     * @param workflowRunId  工作流运行ID
     * @param data           工作流完成数据
     * @return WorkflowFinishedEvent对象
     */
    public static WorkflowFinishedEvent createWorkflowFinishedEvent(String conversationId, String messageId,
                                                                    String taskId, String workflowRunId,
                                                                    WorkflowFinishedData data) {
        WorkflowFinishedEvent event = new WorkflowFinishedEvent();

        // 设置基础属性
        event.setEvent(LlmEventTypeEnum.WORKFLOW_FINISHED.getType());
        event.setConversationId(conversationId);
        event.setMessageId(messageId);
        event.setTaskId(taskId);
        event.setCreatedAt(System.currentTimeMillis());

        // 设置工作流特有属性
        event.setWorkflowRunId(workflowRunId);
        event.setData(data);

        return event;
    }

    /**
     * 创建节点完成事件
     *
     * @param conversationId 会话ID
     * @param messageId      消息ID
     * @param taskId         任务ID
     * @param workflowRunId  工作流运行ID
     * @param data           节点完成数据
     * @return NodeFinishedEvent对象
     */
    public static NodeFinishedEvent createNodeFinishedEvent(String conversationId, String messageId,
                                                            String taskId, String workflowRunId,
                                                            NodeFinishedData data) {
        NodeFinishedEvent event = new NodeFinishedEvent();

        // 设置基础属性
        event.setEvent(LlmEventTypeEnum.NODE_FINISHED.getType());
        event.setConversationId(conversationId);
        event.setMessageId(messageId);
        event.setTaskId(taskId);
        event.setCreatedAt(System.currentTimeMillis());

        // 设置节点特有属性
        event.setWorkflowRunId(workflowRunId);
        event.setData(data);

        return event;
    }

    /**
     * 从LlmChatRespVO转换为对应的LlmEventBase对象
     *
     * @param respVO LlmChatRespVO对象
     * @return 对应的LlmEventBase对象
     */
    public static LlmEventBase fromLlmChatRespVO(LlmChatRespVO respVO) {
        if (respVO == null) {
            return null;
        }

        // 确保基础属性设置正确
        if (respVO.getEvent() == null) {
            respVO.setEvent(respVO.getType().getType());
        }

        return respVO.toLlmEventBase();
    }

    /**
     * 创建错误事件
     *
     * @param conversationId 会话ID
     * @param messageId      消息ID
     * @param taskId         任务ID
     * @param errorMessage   错误消息
     * @return 错误事件对象
     */
    public static LlmEventBase createErrorEvent(String conversationId, String messageId,
                                                String taskId, String errorMessage) {
        LlmChatRespVO errorRespVO = new LlmChatRespVO();
        errorRespVO.setConversationId(conversationId);
        errorRespVO.setMessageId(messageId);
        errorRespVO.setTaskId(taskId);
        errorRespVO.setError(errorMessage);
        errorRespVO.setFinished(true);
        errorRespVO.setCreatedAt(System.currentTimeMillis());
        errorRespVO.setEvent(LlmEventTypeEnum.MESSAGE_END.getType());

        return errorRespVO.toLlmEventBase();
    }
} 